﻿/// <reference path="crafty.js" />
/// <reference path="soapclient.js" />
/// <reference path="json.js" />

//Global Variables
var SCENE_ID_LOADING = "loading";
var SCENE_ID_MAIN = "main";
var ASSET_PATH_CRAFTYSPRITE = "images/Spritesheets/crafty-sprite.png";
var ASSET_PATH_MAZEGRID = "images/MazeImages/15.jpg";
var ASSET_WALL_HORI = "images/Wall/top.jpg";
var ASSET_WALL_VER = "images/Wall/left.jpg";
var ASSET_PLAYER = "images/MazeImages/player.jpg";
var WALL_SIZE = 8;
var BG_LAYER_1 = 1;
var BG_LAYER_2 = 2;
var BG_LAYER_3 = 3;
var BG_LAYER_4 = 4;
var SIDE_UP = 0;
var SIDE_LEFT = 1;
var SIDE_DOWN = 2;
var SIDE_RIGHT = 3;


Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
};

function MazeGrid(posX, posY, wallType) {
    this.posX = posX;
    this.posY = posY;
    this.wallType = wallType;
    this.gridSize = 30;
}

MazeGrid.prototype.IsValidKey = function(key) {
    return this.validKeys.contains(key);
};

function MazeEntity(jsonStr, totalGrid) {
    this.grids = new Array();
    var k = 0;
    for (var i in jsonStr) {
        this.grids[k] = new MazeGrid(jsonStr[i].posX, jsonStr[i].posY, jsonStr[i].wallType);
        k++;
    }
    this.total = totalGrid;
}

function MakeWall(posX, posY, side) {
    var path;
    if (side === SIDE_UP || side === SIDE_DOWN) {
        path = ASSET_WALL_HORI;
    } else {
        path = ASSET_WALL_VER;
    }
    Crafty.e("2D,DOM,Image,solid").attr(
        { x: posX * 30, y: posY * 30, z: BG_LAYER_4 }
    ).image(path);

}


function GameMain(maze_data, height, width) {
    //Fetch map data from maze gen service
    "use strict";
    alert(maze_data);
    var maze_entity = new MazeEntity(JSON.parse(maze_data), height * width);
    Crafty.init(width * 30 + 10, height * 30+10);
    Crafty.canvas.init();


    // Turn the sprite map into usable components
    Crafty.sprite(8, ASSET_PATH_CRAFTYSPRITE, {
        grass1: [0, 0],
        grass2: [1, 0],
        grass3: [2, 0],
        grass4: [3, 0],
        flower: [0, 1],
        bush1: [0, 2],
        bush2: [1, 2],
        player: [0, 3]
    });

    Crafty.sprite([2, 30], ASSET_WALL_HORI, {
        whole_hor_wall: [0, 0]
    });

    Crafty.sprite([30, 2], ASSET_WALL_VER, {
        whole_ver_wall: [0, 0]
    });


    Crafty.sprite(30, ASSET_PATH_MAZEGRID,
        {
            wholeGrid: [0, 0]
        });

    function GenWorld() {
        for (var i = 0; i < maze_entity.total; i++) {
            var x = maze_entity.grids[i].posX;
            var y = maze_entity.grids[i].posY;
            Crafty.e("2D, DOM, wholeGrid")
                .attr({ x: maze_entity.grids[i].posX * 1, y: maze_entity.grids[i].posY * 1, z: BG_LAYER_2 });
            switch (maze_entity.grids[i].wallType) {
            case 0:
                MakeWall(x, y, SIDE_LEFT);
                MakeWall(x, y, SIDE_UP);
                MakeWall(x, y + 1, SIDE_DOWN);
                MakeWall(x + 1, y, SIDE_RIGHT);
                break;
            case 1:
                   // this.validKeys = [Crafty.keys.LEFT_ARROW];
                MakeWall(x, y, SIDE_RIGHT);
                MakeWall(x, y, SIDE_UP);
                MakeWall(x, y + 1, SIDE_DOWN);
                break;
            case 2:
                   // this.validKeys = [Crafty.keys.DOWN_ARROW];
                MakeWall(x, y, SIDE_LEFT);
                MakeWall(x, y, SIDE_UP);
                MakeWall(x + 1, y, SIDE_RIGHT);
                break;
            case 3:
                  //  this.validKeys = [Crafty.keys.LEFT_ARROW, Crafty.keys.DOWN_ARROW];
                MakeWall(x, y, SIDE_UP);
                MakeWall(x + 1, y, SIDE_RIGHT);
                break;
            case 4:
                   // this.validKeys = [Crafty.keys.RIGHT_ARROW];
                MakeWall(x, y, SIDE_LEFT);
                MakeWall(x, y, SIDE_UP);
                MakeWall(x, y + 1, SIDE_DOWN);
                break;
            case 5:
                    //this.validKeys = [Crafty.keys.LEFT_ARROW, Crafty.keys.RIGHT_ARROW];
                MakeWall(x, y, SIDE_UP);
                MakeWall(x, y + 1, SIDE_DOWN);
                break;
            case 6:
                    //this.validKeys = [Crafty.keys.RIGHT_ARROW, Crafty.keys.DOWN_ARROW];
                MakeWall(x, y, SIDE_LEFT);
                MakeWall(x, y, SIDE_UP);
                break;
            case 7:
                    //this.validKeys = [Crafty.keys.RIGHT_ARROW, Crafty.keys.DOWN_ARROW, Crafty.keys.LEFT_ARROW];
                MakeWall(x, y, SIDE_UP);
                break;
            case 8:
                    //this.validKeys = [Crafty.keys.UP_ARROW];
                MakeWall(x, y, SIDE_LEFT);
                MakeWall(x + 1, y, SIDE_RIGHT);
                MakeWall(x, y + 1, SIDE_DOWN);
                break;
            case 9:
                   // this.validKeys = [Crafty.keys.LEFT_ARROW, Crafty.keys.UP_ARROW];
                MakeWall(x + 1, y, SIDE_RIGHT);
                MakeWall(x, y + 1, SIDE_DOWN);
                break;
            case 10:
                   // this.validKeys = [Crafty.keys.UP_ARROW, Crafty.keys.DOWN_ARROW];
                MakeWall(x, y, SIDE_LEFT);
                MakeWall(x + 1, y, SIDE_RIGHT);
                break;
            case 11:
                   // this.validKeys = [Crafty.keys.UP_ARROW, Crafty.keys.DOWN_ARROW, Crafty.keys.LEFT_ARROW];
                MakeWall(x + 1, y, SIDE_RIGHT);
                break;
            case 12:
                    //this.validKeys = [Crafty.keys.UP_ARROW, Crafty.keys.RIGHT_ARROW];
                MakeWall(x, y + 1, SIDE_DOWN);
                MakeWall(x, y, SIDE_LEFT);
                break;
            case 13:
                    //this.validKeys = [Crafty.keys.UP_ARROW, Crafty.keys.LEFT_ARROW, Crafty.keys.RIGHT_ARROW];
                MakeWall(x, y + 1, SIDE_DOWN);
                break;
            case 14:
                    //this.validKeys = [Crafty.keys.UP_ARROW, Crafty.keys.RIGHT_ARROW, Crafty.keys.DOWN_ARROW];
                MakeWall(x, y, SIDE_LEFT);
                break;
            case 15:
                break;
            }
        }
    }


    Crafty.scene(SCENE_ID_LOADING, function() {
        Crafty.load([ASSET_PATH_CRAFTYSPRITE, ASSET_PATH_MAZEGRID, ASSET_WALL_HORI, ASSET_WALL_VER], function() {
            Crafty.scene(SCENE_ID_MAIN);
        });
        var loadindText = "Loading......";
        Crafty.background("#ADD8E6");
        Crafty.e("2D, DOM, Text").attr({ x: width * 30 / 2 - loadindText.length, y: height * 30 / 2 })
            .text(loadindText)
            .css({ "text-align": "center" });
    }
    );
    Crafty.scene(SCENE_ID_LOADING);
    Crafty.c('Ape', {
        Ape: function () {
            this.requires("Collision").onHit("solid", function () {
                console.log("FUCK");
            });
        }
    });
    Crafty.scene(SCENE_ID_MAIN, function () {
        GenWorld();

        Crafty.e("2D, DOM, Image,Multiway,Ape")
        .attr({ x: 4, y:4, z: BG_LAYER_4 })
        .image(ASSET_PLAYER).multiway(1, { UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180 }).Ape();
    });
    
}

;